package org.dromara.system.service.impl;

import org.dromara.system.service.SysMenuService;
import org.dromara.system.pojo.entity.SysMenuDO;
import org.dromara.system.pojo.bo.SysMenuBO;
import org.dromara.system.pojo.qo.SysMenuQO;
import org.dromara.system.pojo.vo.SysMenuVO;
import org.dromara.system.mapper.SysMenuMapper;
import org.springframework.stereotype.Service;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;

import java.util.List;

/**
 * 菜单权限表 服务层实现。
 *
  * @author mkj
  * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysMenuServiceImpl  implements SysMenuService {

    private final SysMenuMapper sysMenuMapper;

    @Override
    public boolean add(SysMenuBO sysMenuBO) {
        SysMenuDO sysMenuDO = BeanCopyUtils.copy(sysMenuBO, SysMenuDO.class);
        return sysMenuMapper.insert(sysMenuDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysMenuMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysMenuBO sysMenuBO) {
        SysMenuDO sysMenuDO = BeanCopyUtils.copy(sysMenuBO, SysMenuDO.class);
        return sysMenuMapper.update(sysMenuDO) > 0;
    }

    @Override
    public List<SysMenuVO> list(SysMenuQO sysMenuQO) {
        SysMenuDO sysMenuDO = BeanCopyUtils.copy(sysMenuQO, SysMenuDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysMenuDO);
        return sysMenuMapper.selectListByQueryAs(queryWrapper, SysMenuVO.class);
    }

    @Override
    public SysMenuVO getInfo(@PathVariable Long id) {
        SysMenuDO sysMenuDO = sysMenuMapper.selectOneById(id);
        if (sysMenuDO == null){
            return null;
        }
        return BeanCopyUtils.copy(sysMenuDO, SysMenuVO.class);
    }

    @Override
    public Page<SysMenuVO> page(SysMenuQO page) {
        return sysMenuMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysMenuVO.class);
    }

}

